package cn.wangchao.user.service.impl;

import cn.wangchao.basic.constants.BaseConstant;
import cn.wangchao.basic.dto.RegisterDto;
import cn.wangchao.basic.exception.BusinessException;
import cn.wangchao.basic.util.MD5Utils;
import cn.wangchao.basic.util.StrUtils;
import cn.wangchao.user.domain.Logininfo;
import cn.wangchao.user.domain.User;
import cn.wangchao.user.mapper.LogininfoMapper;
import cn.wangchao.user.mapper.UserMapper;
import cn.wangchao.user.service.IUserService;
import cn.wangchao.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        //非空判断
        if (StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(smsCode)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(passwordRepeat)
        ){
            throw new BusinessException("参数不能为空哦!");
        }
        //两次密码是否输入一致
        if (!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码输入不一致,请重新输入");
        }
        //短信验证码校验是否正确

        String key = BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + phone;
        Object smsCodeValue = redisTemplate.opsForValue().get(key);
        //判断是否失效,没拿到就是失效
        if (smsCodeValue == null){
            throw new BusinessException("验证码已失效,请重新获取");
        }
        //拿到了就比对验证码
        if (!smsCode.equalsIgnoreCase(smsCodeValue.toString().split(":")[0])){
            //不一样就抛出
            throw new BusinessException("短信验证码不正确,请重新输入");
        }
        //一致就保存数据
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }
    //保存loginof
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        return logininfo;
    }
    //保存user
    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(16);
        String md5 = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5);
        return user;
    }

}
